<?php
/**
 * Hoffman Framework
 *
 * LICENSE
 *
 * This source file is subject to the GNU LGPL 2.1 license that is bundled
 * with this package in the file license.txt.
 * It is also available through the world-wide-web at this URL:
 * http://creativecommons.org/licenses/LGPL/2.1
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to annop@thaicyberpoint.com so we can send you a copy immediately.
 *
 * @package    Hoffman
 * @author     Ford AntiTrust
 * @copyright  Copyright (c) 2008 Ford AntiTrust (http://www.thaicyberpoint.com/ford/blog)
 * @since      Version Rv 0.2
 * @license    http://creativecommons.org/licenses/LGPL/2.1
 *             GNU LGPL 2.1
 * @filesource
 */
if (!defined('INDEX'))
    die('No direct script access allowed.');

/**
 * Httppage
 *
 * @package     Hoffman
 * @subpackage  Hmf
 * @category    Httppage
 * @author      Ford AntiTrust
 * @version     SVN: $Id: Httppage.php 161 2009-10-12 13:52:10Z annop.s@gmail.com $
 * @link        http://www.thaicyberpoint.com/ford/blog/hmf
 */

/**
 * @see Zend_Registry
 */
require_once 'Zend/Registry.php';

class Hmf_Httppage {

    public static function url() {

        $app_config = Zend_Registry::get('app_config');

        $strUrl = null;
        $numArgs = func_num_args();
        if ($numArgs > 0) {
            $args = func_get_args();
            $strUrl = @call_user_func_array(array(Zend_Registry::get('routing_url'), 'getUrl'), $args);
        }
        if(self::isExternalUrl($strUrl)) {
        	return $strUrl;
        }
        if ($app_config['use_clean_url']) {
            return $app_config['base_url'] . '/' . $strUrl;
        } else {
            return $app_config['base_url'] . '/' . 'index.php?qs=' . $strUrl;
        }
    }

    public static function isCurrentUrlAction() {

        $args = func_get_args();
        return (strcmp(Zend_Registry::get('routing_url')->getCurrentUrlAction(), @call_user_func_array(array(Zend_Registry::get('routing_url'), 'getUrl'), $args)) == 0 ? true : false);

    }

    public static function baseUrl($strPath = null, $domain = false) {

        $app_config = Zend_Registry::get('app_config');

        $baseUrl = $app_config['base_url'];

        if ($domain)
            $baseUrl = $app_config['webhost'] . $baseUrl;

        if (is_null($strPath))
            return $baseUrl;

        return $baseUrl . '/' . $strPath;
    }

    public static function link($text, $path, $html = false, array $attributes = array()) {

        // Remove all HTML and PHP tags from a tooltip. For best performance, we act only
        // if a quick strpos() pre-check gave a suspicion (because strip_tags() is expensive).
        if (isset($attributes['title']) && strpos($attributes['title'], '<') !== false) {
            $attributes['title'] = strip_tags($attributes['title']);
        }

        return '<a href="' . Hmf_View::checkPlain($path) . '"' . Hmf_View::attributes($attributes) . '>' . ($html ? $text : Hmf_View::checkPlain($text)) . '</a>';
    }

    public static function isExternalUrl($strUrl) {
    	if (preg_match('@((ht|f)tps?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)|(^\/)@', $strUrl) > 0) {
        	return true;
        }
        return false;
    }

    public static function redirect() {

        $args = func_get_args();

        /* Begin check strUrl is outside from this app and redirect to full url with domain name. */
        if (self::isExternalUrl($args[0]))
            self::redirectToHttp($args[0]);
        /* End */

        $app_config = Zend_Registry::get('app_config');

        $numArgs = func_num_args();

        if ($numArgs > 1) {
            $strUrl = @call_user_func_array(array(Zend_Registry::get('routing_url'), 'getUrl'), $args);
        } elseif ($numArgs == 1) {
            $strUrl = $args[0];
        }

        if ($app_config['use_clean_url']) {
            $strUrl = $app_config['base_url'] . '/' . $strUrl;
        } else {
            $strUrl = $app_config['base_url'] . '/' . 'index.php?qs=' . $strUrl;
        }
        self::redirectToHttp($strUrl);
    }

    public static function redirectToHttp($url) {

        // Send a raw HTTP header
        header("Location: $url");
        exit();
    }

    /*
     - cleans the request variables (ENV, GET, POST, COOKIE, SERVER) and makes sure the query string was parsed correctly.
     */
    public static function FilterQuerystring() {

        $app_config = Zend_Registry::get('app_config');

        $cleanRequest = 0;

        if (!isset($_SERVER['QUERY_STRING']))
            $_SERVER['QUERY_STRING'] = getenv('QUERY_STRING');

        if (strpos(@ini_get('safe_mode'), '1') === FALSE)
            $cleanRequest++;
        else {
            if (@ini_get('arg_separator.input') != $app_config['arg_separator'] or
                @ini_get('arg_separator.output') != $app_config['arg_separator'])
            {
                @ini_set('arg_separator.input', $app_config['arg_separator']);
                @ini_set('arg_separator.output', $app_config['arg_separator']);
                if (@ini_get('arg_separator.input') != $app_config['arg_separator'] or
                    @ini_get('arg_separator.output') != $app_config['arg_separator'])
                {
                    $cleanRequest++;
                }
            }
        }

        if ($cleanRequest > 0) {
            // Get rid of the old one!  You don't know where it's been!
            $_GET = array();
            // Was this redirected?  If so, get the REDIRECT_QUERY_STRING.
            $_SERVER['QUERY_STRING'] = urldecode(substr($_SERVER['QUERY_STRING'], 0, 5) == 'url=/' ? $_SERVER['REDIRECT_QUERY_STRING'] : $_SERVER['QUERY_STRING']);

            // If magic_quotes_gpc isn't off, remove the slashes from the get variables.  (they're gonna be html'd anyway.)
            if (get_magic_quotes_gpc() != 0) {
                $_SERVER['QUERY_STRING'] = stripslashes($_SERVER['QUERY_STRING']);
            }

            // Replace ; with '&' and '&something&' with '&something=&'.  (this is done for compatibility...)
            parse_str(preg_replace('/&(\w+)(&|$)/', '&$1=$2', strtr($_SERVER['QUERY_STRING'], $app_config['arg_separator'], '&')), $_GET);
        }
    }
}
?>